Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_safe_revert: Add a logging statement for IntegrityError #921

Merged
merged 2 commits into from
Oct 2, 2022

Conversation

tony
Copy link
Contributor

@tony tony commented Sep 21, 2022

Fixes #750

This will be welcomed by users explicitly declaring fields with @reversion.register (docs).

Add a logging message for IntegrityError

logging.traceback() offered the perfect level of detail to see which field(s) it's stuck on.

Example logging message
Traceback (most recent call last):
  File "~/projects/django-site/.venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.NotNullViolation: null value in column "weight" of relation "page" violates not-null constraint
DETAIL:  Failing row contains (null, null, 2022-09-21 18:17:24.021+00, 2022-09-21 18:17:24.021+00, 9387e659-c5a4-45d0-ab93-03b0ec354c4b, Page title, Content, <p>..., null, t, f, 13e682c5-a8d4-4aba-a715-16876094d4e1, null, null, null, null, null, null, bb603cb4-5ca3-48db-b7ec-3977db70f784, null).


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "~/projects/python/django-reversion/reversion/models.py", line 34, in _safe_revert
    version.revert()
  File "~/projects/python/django-reversion/reversion/models.py", line 327, in revert
    self._object_version.save(using=self.db)
  File "~/projects/django-site/.venv/lib/python3.10/site-packages/django/core/serializers/base.py", line 223, in save
    models.Model.save_base(self.object, using=using, raw=True, **kwargs)
  File "~/projects/django-site/.venv/lib/python3.10/site-packages/django/db/models/base.py", line 776, in save_base
    updated = self._save_table(
  File "~/projects/django-site/.venv/lib/python3.10/site-packages/django/db/models/base.py", line 858, in _save_table
    updated = self._do_update(base_qs, using, pk_val, values, update_fields,
  File "~/projects/django-site/.venv/lib/python3.10/site-packages/django/db/models/base.py", line 912, in _do_update
    return filtered._update(values) > 0
  File "~/projects/django-site/.venv/lib/python3.10/site-packages/django/db/models/query.py", line 802, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "~/projects/django-site/.venv/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1559, in execute_sql
    cursor = super().execute_sql(result_type)
  File "~/projects/django-site/.venv/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1175, in execute_sql
    cursor.execute(sql, params)
  File "~/projects/django-site/.venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 98, in execute
    return super().execute(sql, params)
  File "~/projects/django-site/.venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "~/projects/django-site/.venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "~/projects/django-site/.venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 79, in _execute
    with self.db.wrap_database_errors:
  File "~/projects/django-site/.venv/lib/python3.10/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "~/projects/django-site/.venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: null value in column "weight" of relation "page" violates not-null constraint
DETAIL:  Failing row contains (null, null, 2022-09-21 18:17:24.021+00, 2022-09-21 18:17:24.021+00, 9387e659-c5a4-45d0-ab93-03b0ec354c4b, Page title, Content, <p>..., null, t, f, 13e682c5-a8d4-4aba-a715-16876094d4e1, null, null, null, null, null, null, bb603cb4-5ca3-48db-b7ec-3977db70f784, null).

Copy link
Owner

@etianen etianen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! I've suggested a tweak to make this less alarming to people.

reversion/models.py Outdated Show resolved Hide resolved
@tony tony requested a review from etianen September 24, 2022 22:49
@tony
Copy link
Contributor Author

tony commented Sep 24, 2022

@etianen How's this?

tony and others added 2 commits September 25, 2022 09:39
This will be helpful @reversion.register users that explicitly declare
their fields.

Fixes etianen#750
Co-authored-by: Dave Hall <dave@etianen.com>
@etianen
Copy link
Owner

etianen commented Oct 2, 2022

Looks great! I've merged!

A release will be incoming shortly.

@etianen etianen closed this Oct 2, 2022
@etianen etianen reopened this Oct 2, 2022
@etianen etianen merged commit 9295e00 into etianen:master Oct 2, 2022
@tony tony deleted the log-integrity-error branch October 2, 2022 14:48
tony added a commit to tony/django-reversion that referenced this pull request Nov 10, 2022
9295e00 was missed, fix for etianen#921's new message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

revert: display information about IntegrityError
2 participants